package dp2.tablet.proyecto;


import dp2.tablet.proyecto.bean.EventoBean;
import dp2.tablet.proyecto.bean.VentaBean;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class Principal extends TabActivity implements OnTabChangeListener{
	private TabHost tabHost;
	
	private VentaBean venta;
	private EventoBean evento;
	
	public static float tipoCambio;	
	
	public static Activity abriCaja=null;
	
	//Venta venta;
	//Pagos pagos;
	
    /** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.principal);
	    Resources res = getResources(); // Resource object to get Drawables
	    tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab
	    

	    venta= null;
	    evento=null;
	    
	    intent = new Intent().setClass(this, Eventos.class);
        spec = tabHost.newTabSpec("eventos").setIndicator("Eventos",
                          res.getDrawable(R.drawable.icon_evento))
                      .setContent(intent);
        tabHost.addTab(spec);
	    
        
	    intent = new Intent().setClass(this, Venta.class);
        spec = tabHost.newTabSpec("ventas").setIndicator("Ventas",
                          res.getDrawable(R.drawable.icon_venta))
                      .setContent(intent);
        tabHost.addTab(spec);
        
        intent = new Intent().setClass(this, Pagos.class);
        spec = tabHost.newTabSpec("pago").setIndicator("Pago",
                          res.getDrawable(R.drawable.icon_pago))
                      .setContent(intent);
        tabHost.addTab(spec);
        
        intent = new Intent().setClass(this, Canjear.class);
        spec = tabHost.newTabSpec("canjear").setIndicator("Canjear",
                         res.getDrawable(R.drawable.icon_canjear))
                      .setContent(intent);
        tabHost.addTab(spec);
        
        //Se ha agregado esa nueva linea a pedido de Pikachu
        intent = new Intent().setClass(this, AnularTicket.class);
        spec = tabHost.newTabSpec("anular").setIndicator("Anular",
                         res.getDrawable(R.drawable.icon_anular))
                      .setContent(intent);
        tabHost.addTab(spec);
        //Aca acaba la linea nueva por culpa de Raichu
        
       intent = new Intent().setClass(this, cierrecaja.class);
        spec = tabHost.newTabSpec("caja").setIndicator("Caja",
                         res.getDrawable(R.drawable.icon_caja))
                     .setContent(intent);
       tabHost.addTab(spec);
	
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
        	tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#00264f")); //unselected
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#294970")); // selected
	   
        // intent = new Intent().setClass(this, Venta.class);
	   // spec = tabHost.newTabSpec("ventas").setIndicator("Ventas")
	   //               .setContent(intent);
	   // tabHost.addTab(spec);
	
	    /*
	    intent = new Intent().setClass(this, CanjesActivity.class);
	    spec = tabHost.newTabSpec("canjear").setIndicator("Canjear")
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    */
	    
	    //intent = new Intent().setClass(this, cierrecaja.class);
	    //spec = tabHost.newTabSpec("caja").setIndicator("Caja").setContent(intent);
	    //tabHost.addTab(spec);
       
	
	    tabHost.setCurrentTab(0);
	    tabHost.setOnTabChangedListener(this);
	    
	    bloquearTabs();
	}

	@Override
	public void onTabChanged(String arg0) {
		// TODO Auto-generated method stub		
		
		//tabHost.getChildAt(1).refreshDrawableState();
		//Pagos dota1 =(Pagos) tabHost.getChildAt(2).getContext();
		//dota1.CargarTabla();
		
		//Venta dota =(Venta)tabHost.getChildAt(2).getContext();
		//dota.loadTableLayout();
		//tabHost.getTabWidget().getChildAt(1).setEnabled(enabled)
		
		for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
			tabHost.getTabWidget().getChildAt(1).setEnabled(true);
			tabHost.getTabWidget().getChildAt(2).setEnabled(true);
        	tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#00264f")); //unselected
        	
        	
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#294970")); // selected
        
        bloquearTabs();

        Activity currentActivity = getCurrentActivity();
        if (currentActivity instanceof Venta) {
            ((Venta) currentActivity).refresh();
        } else  if (currentActivity instanceof Pagos) {
            ((Pagos) currentActivity).CargarTabla();
        }
        else if (currentActivity instanceof cierrecaja){
        	((cierrecaja) currentActivity).refresh();
        }
	}
	
	public void bloquearTabs(){
		if (venta==null){
			tabHost.getTabWidget().getChildAt(1).setEnabled(false);
			tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#7d7d7d"));
			tabHost.getTabWidget().getChildAt(2).setEnabled(false);
			tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#7d7d7d"));
		}
		
	}

	public VentaBean getVenta() {
		return venta;
	}

	public void setVenta(VentaBean venta) {
		this.venta = venta;
	}
	public EventoBean getEvento() {
		return evento;
	}

	public void setEvento(EventoBean evento) {
		this.evento = evento;
	}
}